package com.marketing.tenant.context;

import java.util.List;
import java.util.Set;

/**
 * 用户上下文类
 * 存储当前请求的用户信息
 */
public class UserContext {
    
    private static final ThreadLocal<UserInfo> USER_THREAD_LOCAL = new ThreadLocal<>();
    
    /**
     * 设置当前用户信息
     */
    public static void setUser(UserInfo userInfo) {
        USER_THREAD_LOCAL.set(userInfo);
    }
    
    /**
     * 获取当前用户信息
     */
    public static UserInfo getUser() {
        return USER_THREAD_LOCAL.get();
    }
    
    /**
     * 获取当前用户ID
     */
    public static Long getUserId() {
        UserInfo userInfo = getUser();
        return userInfo != null ? userInfo.getUserId() : null;
    }
    
    /**
     * 获取当前用户名
     */
    public static String getUsername() {
        UserInfo userInfo = getUser();
        return userInfo != null ? userInfo.getUsername() : null;
    }
    
    /**
     * 获取当前用户主要角色
     */
    public static String getRole() {
        UserInfo userInfo = getUser();
        return userInfo != null ? userInfo.getRole() : null;
    }
    
    /**
     * 获取当前用户所有角色
     */
    public static List<String> getRoles() {
        UserInfo userInfo = getUser();
        return userInfo != null ? userInfo.getRoles() : null;
    }
    
    /**
     * 获取当前用户权限
     */
    public static Set<String> getPermissions() {
        UserInfo userInfo = getUser();
        return userInfo != null ? userInfo.getPermissions() : null;
    }
    
    /**
     * 获取当前租户ID
     */
    public static Long getTenantId() {
        UserInfo userInfo = getUser();
        return userInfo != null ? userInfo.getTenantId() : null;
    }
    
    /**
     * 检查当前用户是否有指定权限
     */
    public static boolean hasPermission(String permission) {
        Set<String> permissions = getPermissions();
        return permissions != null && permissions.contains(permission);
    }
    
    /**
     * 检查当前用户是否有指定角色
     */
    public static boolean hasRole(String role) {
        List<String> roles = getRoles();
        return roles != null && roles.contains(role);
    }
    
    /**
     * 清除当前用户信息
     */
    public static void clear() {
        USER_THREAD_LOCAL.remove();
    }
    
    /**
     * 用户信息内部类
     */
    public static class UserInfo {
        private Long userId;
        private String username;
        private String role;
        private List<String> roles;
        private Set<String> permissions;
        private Long tenantId;  // 租户ID
        
        public UserInfo() {}
        
        public UserInfo(Long userId, String username, String role, List<String> roles, Set<String> permissions) {
            this.userId = userId;
            this.username = username;
            this.role = role;
            this.roles = roles;
            this.permissions = permissions;
        }
        
        public UserInfo(Long userId, String username, String role, List<String> roles, Set<String> permissions, Long tenantId) {
            this.userId = userId;
            this.username = username;
            this.role = role;
            this.roles = roles;
            this.permissions = permissions;
            this.tenantId = tenantId;
        }
        
        // Getters and Setters
        public Long getUserId() {
            return userId;
        }
        
        public void setUserId(Long userId) {
            this.userId = userId;
        }
        
        public String getUsername() {
            return username;
        }
        
        public void setUsername(String username) {
            this.username = username;
        }
        
        public String getRole() {
            return role;
        }
        
        public void setRole(String role) {
            this.role = role;
        }
        
        public List<String> getRoles() {
            return roles;
        }
        
        public void setRoles(List<String> roles) {
            this.roles = roles;
        }
        
        public Set<String> getPermissions() {
            return permissions;
        }
        
        public void setPermissions(Set<String> permissions) {
            this.permissions = permissions;
        }
        
        public Long getTenantId() {
            return tenantId;
        }
        
        public void setTenantId(Long tenantId) {
            this.tenantId = tenantId;
        }
        
        @Override
        public String toString() {
            return "UserInfo{" +
                    "userId=" + userId +
                    ", username='" + username + '\'' +
                    ", role='" + role + '\'' +
                    ", roles=" + roles +
                    ", permissions=" + permissions +
                    ", tenantId=" + tenantId +
                    '}';
        }
    }
}